/*     */ package com.zimbra.qa.unittest;
/*     */ 
/*     */ import com.zimbra.client.ZEmailAddress;
/*     */ import com.zimbra.client.ZFolder;
/*     */ import com.zimbra.client.ZGetMessageParams;
/*     */ import com.zimbra.client.ZMailbox;
/*     */ import com.zimbra.client.ZMailbox.ZOutgoingMessage;
/*     */ import com.zimbra.client.ZMailbox.ZOutgoingMessage.MessagePart;
/*     */ import com.zimbra.client.ZMessage;
/*     */ import com.zimbra.client.ZMessage.ZMimePart;
/*     */ import com.zimbra.common.lmtp.LmtpClient;
/*     */ import com.zimbra.common.localconfig.KnownKey;
/*     */ import com.zimbra.common.localconfig.LC;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.util.ByteUtil;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.account.Config;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import com.zimbra.cs.account.Server;
/*     */ import com.zimbra.cs.lmtpserver.LmtpMessageInputStream;
/*     */ import java.io.ByteArrayInputStream;
/*     */ import java.io.IOException;
/*     */ import java.io.InputStream;
/*     */ import java.util.ArrayList;
/*     */ import java.util.HashMap;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ import junit.framework.TestCase;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class TestLmtp
/*     */   extends TestCase
/*     */ {
/*     */   private static final String USER_NAME = "user1";
/*     */   private static final String USER2_NAME = "user2";
/*  54 */   private static final String NAME_PREFIX = TestLmtp.class.getSimpleName();
/*     */   
/*     */   private static final String STARTTLS = "STARTTLS";
/*     */   private static final String NOOP = "NOOP";
/*     */   private static final String RSET = "RSET";
/*     */   private static final String VRFY = "VRFY";
/*     */   private ZMailbox mbox;
/*     */   private Account account;
/*     */   private String originalWarnInterval;
/*     */   private int originalWarnPercent;
/*     */   private String originalServerDiskThreshold;
/*     */   private String originalConfigDiskThreshold;
/*     */   private String originalQuota;
/*     */   private String originalAllowReceiveButNotSendWhenOverQuota;
/*     */   private String originalDedupeCacheSize;
/*     */   private String originalDedupeCacheTimeout;
/*     */   private String originalDedupingEnabled;
/*     */   
/*     */   private class LmtpClientThread
/*     */     implements Runnable
/*     */   {
/*     */     private String mRecipient;
/*     */     private String mContent;
/*     */     
/*     */     private LmtpClientThread(String recipient, String content)
/*     */     {
/*  80 */       this.mRecipient = recipient;
/*  81 */       this.mContent = content;
/*     */     }
/*     */     
/*     */     public void run()
/*     */     {
/*     */       try {
/*  87 */         TestUtil.addMessageLmtp(new String[] { this.mRecipient }, this.mRecipient, this.mContent);
/*     */       } catch (Exception e) {
/*  89 */         ZimbraLog.test.error("Unable to send message to %s.", this.mRecipient, e);
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */   public void setUp()
/*     */     throws Exception
/*     */   {
/*  97 */     this.mbox = TestUtil.getZMailbox("user1");
/*  98 */     this.account = TestUtil.getAccount("user1");
/*  99 */     this.originalWarnInterval = this.account.getAttr("zimbraQuotaWarnInterval");
/* 100 */     this.originalWarnPercent = this.account.getIntAttr("zimbraQuotaWarnPercent", 0);
/* 101 */     this.originalServerDiskThreshold = TestUtil.getServerAttr("zimbraMailDiskStreamingThreshold");
/*     */     
/* 103 */     this.originalConfigDiskThreshold = TestUtil.getConfigAttr("zimbraMailDiskStreamingThreshold");
/*     */     
/* 105 */     this.originalQuota = TestUtil.getAccountAttr("user1", "zimbraMailQuota");
/* 106 */     this.originalAllowReceiveButNotSendWhenOverQuota = TestUtil.getAccountAttr("user1", "zimbraMailAllowReceiveButNotSendWhenOverQuota");
/*     */     
/* 108 */     this.originalDedupeCacheSize = TestUtil.getConfigAttr("zimbraMessageIdDedupeCacheSize");
/* 109 */     this.originalDedupeCacheTimeout = TestUtil.getConfigAttr("zimbraMessageIdDedupeCacheTimeout");
/* 110 */     this.originalDedupingEnabled = TestUtil.getAccountAttr("user1", "zimbraPrefMessageIdDedupingEnabled");
/* 111 */     cleanUp();
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public void testReadLmtpData()
/*     */     throws Exception
/*     */   {
/* 121 */     assertEquals("123", read("123", 3, 3));
/* 122 */     assertEquals("123", read("123", -1, 3));
/* 123 */     assertEquals("123", read("123", 0, 3));
/* 124 */     assertEquals("123", read("123", 10, 3));
/* 125 */     assertEquals("123", read("123", 3, 10));
/*     */     
/*     */ 
/* 128 */     assertEquals("12", read("123", -1, 2));
/* 129 */     assertEquals("12", read("123", 0, 2));
/* 130 */     assertEquals("12", read("123", 1, 2));
/* 131 */     assertEquals("12", read("123", 2, 2));
/* 132 */     assertEquals("12", read("123", 10, 2));
/*     */   }
/*     */   
/*     */   private String read(String dataString, int sizeHint, int limit) throws Exception
/*     */   {
/* 137 */     byte[] data = dataString.getBytes();
/* 138 */     int numToRead = Math.min(data.length, limit);
/* 139 */     int numRemaining = data.length - numToRead;
/* 140 */     byte[] expected = new byte[numToRead];
/* 141 */     System.arraycopy(data, 0, expected, 0, numToRead);
/* 142 */     InputStream in = new ByteArrayInputStream(data);
/*     */     
/* 144 */     byte[] bytesRead = ByteUtil.readInput(in, sizeHint, limit);
/* 145 */     assertEquals(numToRead, bytesRead.length);
/* 146 */     assertEquals(numRemaining, in.available());
/* 147 */     assertEquals(new String(expected), new String(bytesRead));
/* 148 */     if (numRemaining == 0) {
/* 149 */       assertEquals(-1, in.read());
/*     */     } else {
/* 151 */       assertTrue(in.read() >= 0);
/*     */     }
/* 153 */     return new String(bytesRead);
/*     */   }
/*     */   
/*     */   public void testQuotaWarning()
/*     */     throws Exception
/*     */   {
/* 159 */     Account account = TestUtil.getAccount("user1");
/* 160 */     account.setQuotaLastWarnTimeAsString("");
/* 161 */     ZMailbox mbox = TestUtil.getZMailbox("user1");
/*     */     
/*     */ 
/*     */ 
/* 165 */     account.setMailQuota(Math.max(mbox.getSize() * 2L, 10000L));
/*     */     
/*     */ 
/* 168 */     validateNumWarnings(0);
/*     */     
/*     */ 
/* 171 */     TestUtil.addMessageLmtp(NAME_PREFIX + " testQuotaWarning 1", "user1", "user1");
/* 172 */     validateNumWarnings(0);
/*     */     
/*     */ 
/* 175 */     setQuotaWarnPercent(0);
/* 176 */     TestUtil.addMessageLmtp(NAME_PREFIX + " testQuotaWarning 2", "user1", "user1");
/* 177 */     validateNumWarnings(0);
/*     */     
/*     */ 
/* 180 */     setQuotaWarnPercent(0);
/* 181 */     TestUtil.addMessageLmtp(NAME_PREFIX + " testQuotaWarning 3", "user1", "user1");
/* 182 */     validateNumWarnings(0);
/*     */     
/*     */ 
/* 185 */     setQuotaWarnPercent(1);
/* 186 */     TestUtil.addMessageLmtp(NAME_PREFIX + " testQuotaWarning 4", "user1", "user1");
/* 187 */     validateNumWarnings(1);
/*     */     
/*     */ 
/* 190 */     TestUtil.addMessageLmtp(NAME_PREFIX + " testQuotaWarning 5", "user1", "user1");
/* 191 */     validateNumWarnings(1);
/*     */     
/*     */ 
/* 194 */     setQuotaWarnInterval("1s");
/* 195 */     Thread.sleep(1000L);
/* 196 */     TestUtil.addMessageLmtp(NAME_PREFIX + " testQuotaWarning 6", "user1", "user1");
/* 197 */     validateNumWarnings(2);
/*     */     
/*     */ 
/*     */ 
/* 201 */     setQuotaWarnInterval("");
/* 202 */     TestUtil.addMessageLmtp(NAME_PREFIX + " testQuotaWarning 7", "user1", "user1");
/* 203 */     validateNumWarnings(2);
/*     */   }
/*     */   
/*     */   private void validateNumWarnings(int numWarnings) throws Exception
/*     */   {
/* 208 */     List<ZMessage> messages = TestUtil.search(this.mbox, "Quota warning");
/* 209 */     assertEquals("Number of quota warnings", numWarnings, messages.size());
/*     */   }
/*     */   
/*     */   private void setQuotaWarnPercent(int percent) throws Exception
/*     */   {
/* 214 */     Map<String, String> attrs = new HashMap();
/* 215 */     attrs.put("zimbraQuotaWarnPercent", Integer.toString(percent));
/* 216 */     Provisioning.getInstance().modifyAttrs(this.account, attrs);
/*     */   }
/*     */   
/*     */   private void setQuotaWarnInterval(String interval) throws Exception
/*     */   {
/* 221 */     Map<String, String> attrs = new HashMap();
/* 222 */     attrs.put("zimbraQuotaWarnInterval", interval);
/* 223 */     Provisioning.getInstance().modifyAttrs(this.account, attrs);
/*     */   }
/*     */   
/*     */   public void testLmtpMessageInputStream() throws Exception
/*     */   {
/* 228 */     String prefix = "12345678\r\n";
/*     */     
/*     */ 
/* 231 */     runLmtpMessageTest("abcd\r\n", null, null);
/* 232 */     runLmtpMessageTest("abcd\r\n.\r", null, null);
/* 233 */     runLmtpMessageTest("\n\r.\r\n", null, null);
/* 234 */     runLmtpMessageTest("\n\r\r\r\r\n\r.\r\n", null, null);
/* 235 */     runLmtpMessageTest("\r\n.\n\r\n", null, null);
/* 236 */     runLmtpMessageTest("..\r\n", null, null);
/* 237 */     runLmtpMessageTest(".", null, null);
/* 238 */     runLmtpMessageTest(".\r", null, null);
/*     */     
/*     */ 
/* 241 */     runLmtpMessageTest("ab\r\ncd\r\n.\r\n", "ab\r\ncd\r\n", null);
/* 242 */     runLmtpMessageTest("ab\r\ncd\r\n.\r\n", "ab\r\ncd\r\n", prefix);
/* 243 */     runLmtpMessageTest("ab\r\ncd\r\n\r\n.\r\n", "ab\r\ncd\r\n\r\n", null);
/* 244 */     runLmtpMessageTest("ab\r\ncd\r\n\r\n.\r\n", "ab\r\ncd\r\n\r\n", prefix);
/* 245 */     runLmtpMessageTest("ab\r\n..\n\r\n\r\n.\r\n", "ab\r\n.\n\r\n\r\n", null);
/* 246 */     runLmtpMessageTest("ab\r\n..\n\r\n\r\n.\r\n", "ab\r\n.\n\r\n\r\n", prefix);
/* 247 */     runLmtpMessageTest("ab\r\n.\rcd\r\n.\r\n", "ab\r\n\rcd\r\n", null);
/* 248 */     runLmtpMessageTest("ab\r\n.\rcd\r\n.\r\n", "ab\r\n\rcd\r\n", prefix);
/*     */     
/*     */ 
/* 251 */     runLmtpMessageTest(".\r\n", "", null);
/* 252 */     runLmtpMessageTest(".\r\n", "", prefix);
/* 253 */     runLmtpMessageTest("..\r\n.\r\n", ".\r\n", null);
/* 254 */     runLmtpMessageTest("..\r\n.\r\n", ".\r\n", prefix);
/* 255 */     runLmtpMessageTest("..\rabcd\r\n.\r\n", ".\rabcd\r\n", null);
/* 256 */     runLmtpMessageTest("..\rabcd\r\n.\r\n", ".\rabcd\r\n", prefix);
/* 257 */     runLmtpMessageTest("..a\r\n.\r\n", ".a\r\n", null);
/* 258 */     runLmtpMessageTest("..a\r\n.\r\n", ".a\r\n", prefix);
/* 259 */     runLmtpMessageTest("a\r\n..a\r\n.\r\n", "a\r\n.a\r\n", null);
/* 260 */     runLmtpMessageTest("a\r\n..a\r\n.\r\n", "a\r\n.a\r\n", prefix);
/*     */   }
/*     */   
/*     */   private void runLmtpMessageTest(String input, String expectedOutput, String prefix)
/*     */     throws Exception
/*     */   {
/* 266 */     ByteArrayInputStream in = new ByteArrayInputStream(input.getBytes());
/* 267 */     LmtpMessageInputStream lin = new LmtpMessageInputStream(in, prefix);
/*     */     
/* 269 */     StringBuilder readContent = new StringBuilder();
/*     */     try {
/*     */       int c;
/* 272 */       while ((c = lin.read()) >= 0) {
/* 273 */         readContent.append((char)c);
/*     */       }
/*     */     } catch (IOException ioe) {
/* 276 */       if (expectedOutput == null) {
/* 277 */         return;
/*     */       }
/* 279 */       throw ioe;
/*     */     }
/*     */     
/*     */ 
/* 283 */     if (prefix == null) {
/* 284 */       prefix = "";
/*     */     }
/* 286 */     assertEquals(prefix + expectedOutput, readContent.toString());
/* 287 */     assertEquals(expectedOutput.length() + prefix.length(), lin.getMessageSize());
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   public void testDiskStreamingOneRecipient()
/*     */     throws Exception
/*     */   {
/* 295 */     TestUtil.setServerAttr("zimbraMailDiskStreamingThreshold", "0");
/* 296 */     String recipient = TestUtil.getAddress("user1");
/* 297 */     TestUtil.addMessageLmtp(NAME_PREFIX + " testDiskStreamingOneRecipient", recipient, TestUtil.getAddress("user1"));
/* 298 */     ZMailbox mbox = TestUtil.getZMailbox("user1");
/* 299 */     TestUtil.waitForMessage(mbox, NAME_PREFIX);
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   public void testDiskStreamingMultipleRecipients()
/*     */     throws Exception
/*     */   {
/* 307 */     TestUtil.setServerAttr("zimbraMailDiskStreamingThreshold", "0");
/* 308 */     String[] recipients = { TestUtil.getAddress("user1"), TestUtil.getAddress("user2") };
/*     */     
/*     */ 
/*     */ 
/*     */ 
/* 313 */     String subject = NAME_PREFIX + " testDiskStreamingMultipleRecipients";
/* 314 */     ZMailbox mbox1 = TestUtil.getZMailbox("user1");
/* 315 */     ZMailbox mbox2 = TestUtil.getZMailbox("user2");
/*     */     
/* 317 */     TestUtil.addMessageLmtp(subject, recipients, TestUtil.getAddress("user1"));
/* 318 */     TestUtil.waitForMessage(mbox1, "in:inbox subject:\"" + subject + "\"");
/* 319 */     ZMessage msg2 = TestUtil.waitForMessage(mbox2, "in:inbox subject:\"" + subject + "\"");
/*     */     
/*     */ 
/*     */ 
/* 323 */     mbox2.deleteMessage(msg2.getId());
/* 324 */     mbox1 = TestUtil.getZMailbox("user1");
/* 325 */     TestUtil.waitForMessage(mbox1, "in:inbox subject:\"" + subject + "\"");
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   public void testDiskStreamingEmptyFolder()
/*     */     throws Exception
/*     */   {
/* 334 */     TestUtil.setServerAttr("zimbraMailDiskStreamingThreshold", "0");
/* 335 */     String[] recipients = { TestUtil.getAddress("user1"), TestUtil.getAddress("user2") };
/*     */     
/*     */ 
/*     */ 
/*     */ 
/* 340 */     String subject = NAME_PREFIX + " testDiskStreamingMultipleRecipients";
/* 341 */     ZMailbox mbox1 = TestUtil.getZMailbox("user1");
/* 342 */     ZMailbox mbox2 = TestUtil.getZMailbox("user2");
/*     */     
/* 344 */     TestUtil.addMessageLmtp(subject, recipients, TestUtil.getAddress("user1"));
/* 345 */     TestUtil.waitForMessage(mbox1, "in:inbox subject:\"" + subject + "\"");
/* 346 */     ZMessage msg2 = TestUtil.waitForMessage(mbox2, "in:inbox subject:\"" + subject + "\"");
/*     */     
/*     */ 
/*     */ 
/* 350 */     ZFolder folder2 = TestUtil.createFolder(mbox2, "/" + NAME_PREFIX + " testDiskStreamingEmptyFolder");
/* 351 */     mbox2.moveMessage(msg2.getId(), folder2.getId());
/*     */     
/*     */ 
/*     */ 
/* 355 */     mbox2.markItemRead(msg2.getId(), true, null);
/* 356 */     mbox2.emptyFolder(folder2.getId());
/* 357 */     mbox1 = TestUtil.getZMailbox("user1");
/* 358 */     TestUtil.waitForMessage(mbox1, "in:inbox subject:\"" + subject + "\"");
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   public void testSizeHint()
/*     */     throws Exception
/*     */   {
/* 367 */     String address = TestUtil.getAddress("user1");
/* 368 */     String subject = NAME_PREFIX + " testIncorrectSizeHint";
/* 369 */     String messageString = TestUtil.getTestMessage(subject, address, address, null);
/* 370 */     String[] recipients = { address };
/* 371 */     LmtpClient lmtp = new LmtpClient("localhost", 7025);
/* 372 */     byte[] data = messageString.getBytes();
/*     */     
/* 374 */     lmtp.sendMessage(new ByteArrayInputStream(data), recipients, address, "TestLmtp", null);
/* 375 */     lmtp.sendMessage(new ByteArrayInputStream(data), recipients, address, "TestLmtp", Long.valueOf(0L));
/* 376 */     lmtp.sendMessage(new ByteArrayInputStream(data), recipients, address, "TestLmtp", Long.valueOf(10L));
/* 377 */     lmtp.sendMessage(new ByteArrayInputStream(data), recipients, address, "TestLmtp", Long.valueOf(data.length));
/* 378 */     lmtp.sendMessage(new ByteArrayInputStream(data), recipients, address, "TestLmtp", Long.valueOf(2147483647L));
/* 379 */     lmtp.close();
/*     */     
/* 381 */     ZMailbox mbox = TestUtil.getZMailbox("user1");
/* 382 */     List<ZMessage> messages = TestUtil.search(mbox, subject);
/* 383 */     assertEquals(5, messages.size());
/*     */     
/*     */ 
/* 386 */     ZGetMessageParams params = new ZGetMessageParams();
/* 387 */     params.setRawContent(true);
/* 388 */     for (ZMessage msg : messages) {
/* 389 */       String content = TestUtil.getContent(mbox, msg.getId());
/* 390 */       TestUtil.assertMessageContains(content, messageString);
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public void testAttachedMessage()
/*     */     throws Exception
/*     */   {
/* 401 */     String outerSubject = NAME_PREFIX + " testAttachedMessage outer";
/* 402 */     String innerSubject = NAME_PREFIX + " testAttachedMessage inner";
/*     */     
/* 404 */     ZMailbox mbox = TestUtil.getZMailbox("user1");
/*     */     
/*     */ 
/* 407 */     ZMailbox.ZOutgoingMessage msg = new ZMailbox.ZOutgoingMessage();
/* 408 */     List<ZEmailAddress> addresses = new ArrayList();
/* 409 */     addresses.add(new ZEmailAddress(TestUtil.getAddress("user1"), null, null, "t"));
/*     */     
/* 411 */     msg.setAddresses(addresses);
/* 412 */     msg.setSubject(outerSubject);
/*     */     
/*     */ 
/* 415 */     String attachedMessageString = TestUtil.getTestMessage(innerSubject, "user1", "user1", null);
/* 416 */     ZMailbox.ZOutgoingMessage.MessagePart attachedMessage = new ZMailbox.ZOutgoingMessage.MessagePart("message/rfc822", attachedMessageString);
/* 417 */     ZMailbox.ZOutgoingMessage.MessagePart body = new ZMailbox.ZOutgoingMessage.MessagePart("text/plain", "This is the outer message");
/* 418 */     msg.setMessagePart(new ZMailbox.ZOutgoingMessage.MessagePart("multipart/mixed", new ZMailbox.ZOutgoingMessage.MessagePart[] { body, attachedMessage }));
/*     */     
/*     */ 
/* 421 */     mbox.sendMessage(msg, null, false);
/* 422 */     TestUtil.waitForMessage(mbox, "in:inbox " + outerSubject);
/*     */     
/*     */ 
/* 425 */     List<ZMessage> msgs = TestUtil.search(mbox, "in:inbox " + innerSubject);
/* 426 */     assertEquals(1, msgs.size());
/* 427 */     msgs = TestUtil.search(mbox, "in:sent " + innerSubject);
/* 428 */     assertEquals(1, msgs.size());
/*     */     
/*     */ 
/* 431 */     msgs = TestUtil.search(mbox, "in:inbox " + NAME_PREFIX + " waves");
/* 432 */     assertEquals(1, msgs.size());
/* 433 */     msgs = TestUtil.search(mbox, "in:sent " + NAME_PREFIX + " waves");
/* 434 */     assertEquals(1, msgs.size());
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   public void testMissingDiskThreshold()
/*     */     throws Exception
/*     */   {
/* 443 */     TestUtil.setServerAttr("zimbraMailDiskStreamingThreshold", "");
/* 444 */     TestUtil.setConfigAttr("zimbraMailDiskStreamingThreshold", "");
/* 445 */     String subject = NAME_PREFIX + " testMissingDiskThreshold";
/* 446 */     TestUtil.addMessageLmtp(subject, "user1", "user1");
/* 447 */     ZMailbox mbox = TestUtil.getZMailbox("user1");
/* 448 */     TestUtil.waitForMessage(mbox, "in:inbox subject:\"" + subject + "\"");
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public void testConcurrentDedupe()
/*     */     throws Exception
/*     */   {
/* 458 */     String subject = NAME_PREFIX + " testConcurrentDedupe";
/* 459 */     String content = TestUtil.getTestMessage(subject, "user1", "user1", null);
/* 460 */     content = "Message-ID: " + System.currentTimeMillis() + "\r\n" + content;
/*     */     
/* 462 */     Thread[] threads = new Thread[5];
/* 463 */     for (int i = 0; i < threads.length; i++) {
/* 464 */       threads[i] = new Thread(new LmtpClientThread("user1", content, null));
/*     */     }
/* 466 */     for (Thread thread : threads) {
/* 467 */       thread.start();
/*     */     }
/* 469 */     for (Thread thread : threads) {
/* 470 */       thread.join();
/*     */     }
/* 472 */     ZMailbox mbox = TestUtil.getZMailbox("user1");
/* 473 */     List<ZMessage> messages = TestUtil.search(mbox, "in:inbox subject:\"" + subject + "\"");
/* 474 */     assertEquals(1, messages.size());
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public void testDeliveryAfterFailure()
/*     */     throws Exception
/*     */   {
/* 485 */     String subject = NAME_PREFIX + " testDeliveryAfterFailure";
/* 486 */     String content = TestUtil.getTestMessage(subject, "user1", "user1", null);
/* 487 */     content = "Message-ID: " + System.currentTimeMillis() + "\r\n" + content;
/* 488 */     String[] recipients = { "user1" };
/*     */     
/*     */ 
/* 491 */     TestUtil.setAccountAttr("user1", "zimbraMailQuota", "1");
/* 492 */     assertFalse("LMTP should not have succeeded", TestUtil.addMessageLmtp(recipients, "user1", content));
/*     */     
/*     */ 
/* 495 */     TestUtil.setAccountAttr("user1", "zimbraMailQuota", this.originalQuota);
/* 496 */     TestUtil.addMessageLmtp(recipients, "user1", content);
/* 497 */     ZMailbox mbox = TestUtil.getZMailbox("user1");
/* 498 */     TestUtil.getMessage(mbox, "in:inbox subject:\"" + subject + "\"");
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   public void testDedupePref()
/*     */     throws Exception
/*     */   {
/* 506 */     String subject = NAME_PREFIX + " testDedupePref";
/* 507 */     ZMailbox mbox = TestUtil.getZMailbox("user1");
/* 508 */     Account account = TestUtil.getAccount("user1");
/* 509 */     String[] recipients = { "user1" };
/*     */     
/*     */ 
/* 512 */     String content = new MessageBuilder().withSubject(subject).withToRecipient("user1").withFrom("user1").withMessageIdHeader().create();
/*     */     
/* 514 */     TestUtil.addMessageLmtp(recipients, "user1", content);
/*     */     
/* 516 */     String query = "in:inbox subject:\"" + subject + "\"";
/*     */     
/*     */ 
/* 519 */     account.setPrefMessageIdDedupingEnabled(true);
/* 520 */     TestUtil.addMessageLmtp(recipients, "user1", content);
/* 521 */     assertEquals(1, TestUtil.search(mbox, query).size());
/*     */     
/*     */ 
/* 524 */     account.setPrefMessageIdDedupingEnabled(false);
/* 525 */     TestUtil.addMessageLmtp(recipients, "user1", content);
/* 526 */     assertEquals(2, TestUtil.search(mbox, query).size());
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   public void disableTestDedupeCacheTimeout()
/*     */     throws Exception
/*     */   {
/* 535 */     ZMailbox mbox = TestUtil.getZMailbox("user1");
/*     */     
/*     */ 
/* 538 */     String subject = NAME_PREFIX + " testDedupeCacheTimeout";
/* 539 */     String[] recipients = { "user1" };
/* 540 */     String content = new MessageBuilder().withSubject(subject).withToRecipient("user1").withFrom("user1").withMessageIdHeader().create();
/*     */     
/* 542 */     TestUtil.addMessageLmtp(recipients, "user1", content);
/* 543 */     String query = "in:inbox subject:\"" + subject + "\"";
/* 544 */     assertEquals("message should have been delivered", 1, TestUtil.search(mbox, query).size());
/*     */     
/*     */ 
/* 547 */     TestUtil.setConfigAttr("zimbraMessageIdDedupeCacheTimeout", "500ms");
/*     */     
/*     */ 
/* 550 */     TestUtil.addMessageLmtp(recipients, "user1", content);
/* 551 */     assertEquals("deduping should have happened", 1, TestUtil.search(mbox, query).size());
/*     */     
/*     */ 
/* 554 */     Thread.sleep(501L);
/*     */     
/*     */ 
/* 557 */     TestUtil.addMessageLmtp(recipients, "user1", content);
/* 558 */     assertEquals("dedupe cache entry should have timed out", 2, TestUtil.search(mbox, query).size());
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public void testValidation()
/*     */     throws Exception
/*     */   {
/* 568 */     StringBuilder buf = new StringBuilder();
/* 569 */     for (int i = 0; i <= LC.zimbra_lmtp_max_line_length.longValue(); i++) {
/* 570 */       buf.append('x');
/*     */     }
/* 572 */     assertFalse(TestUtil.addMessageLmtp(new String[] { "user1" }, "user1", buf.toString()));
/*     */   }
/*     */   
/*     */   public void testTransparency() throws Exception {
/* 576 */     String subject = NAME_PREFIX + " LMTPTransparency1";
/* 577 */     String body = "line1\r\n.line2\r\n..line3\r\n...line4\r\n";
/* 578 */     assertTrue(TestUtil.addMessageLmtp(subject, new String[] { "user1" }, "user1", body));
/* 579 */     ZMailbox mbox = TestUtil.getZMailbox("user1");
/* 580 */     ZMessage msg = TestUtil.getMessage(mbox, "in:inbox subject:\"" + subject + "\"");
/* 581 */     String currentBody = msg.getMimeStructure().getContent();
/* 582 */     TestUtil.assertMessageContains(currentBody, body);
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   public void testAllowReceiveButNotSendWhenOverQuota()
/*     */     throws Exception
/*     */   {
/* 590 */     TestUtil.setAccountAttr("user1", "zimbraMailAllowReceiveButNotSendWhenOverQuota", "TRUE");
/* 591 */     TestUtil.setAccountAttr("user1", "zimbraMailQuota", "1");
/* 592 */     String subject = NAME_PREFIX + " testAllowReceiveButNotSendWhenOverQuota";
/*     */     
/*     */ 
/* 595 */     TestUtil.addMessageLmtp(subject, "user1", "user1");
/* 596 */     ZMailbox mbox = TestUtil.getZMailbox("user1");
/* 597 */     TestUtil.getMessage(mbox, "in:inbox subject:\"" + subject + "\"");
/*     */     
/*     */     try
/*     */     {
/* 601 */       TestUtil.sendMessage(mbox, "user1", subject);
/* 602 */       fail("Send should have failed");
/*     */     } catch (ServiceException e) {
/* 604 */       assertEquals("mail.QUOTA_EXCEEDED", e.getCode());
/*     */     }
/*     */     
/*     */     try
/*     */     {
/* 609 */       byte[] data = new byte['Ѐ'];
/* 610 */       TestUtil.createDocument(mbox, Integer.toString(16), NAME_PREFIX + " receivenosend.bin", "application/content-stream", data);
/* 611 */       fail("Document creation should have failed");
/*     */     } catch (ServiceException e) {
/* 613 */       assertEquals("mail.QUOTA_EXCEEDED", e.getCode());
/*     */     }
/*     */     
/*     */ 
/* 617 */     String draftSubject1 = subject + " save draft 1";
/* 618 */     String draftSubject2 = subject + " save draft 2 two";
/* 619 */     ZMailbox.ZOutgoingMessage outgoingDraft = TestUtil.getOutgoingMessage("user1", draftSubject1, draftSubject1, null);
/* 620 */     ZMessage draftMsg = mbox.saveDraft(outgoingDraft, null, Integer.toString(6));
/* 621 */     outgoingDraft = TestUtil.getOutgoingMessage("user1", draftSubject2, draftSubject2, null);
/* 622 */     mbox.saveDraft(outgoingDraft, draftMsg.getId(), Integer.toString(6));
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   public void testResentMessageId()
/*     */     throws Exception
/*     */   {
/* 631 */     Provisioning.getInstance().getConfig().setMessageIdDedupeCacheSize(1000);
/*     */     
/*     */ 
/* 634 */     ZMailbox mbox = TestUtil.getZMailbox("user1");
/* 635 */     String subject = NAME_PREFIX + " testResentMessageId";
/* 636 */     String content = TestUtil.getTestMessage(subject, "user1", "user1", null);
/* 637 */     content = "Message-ID: " + System.currentTimeMillis() + "\r\n" + content;
/* 638 */     String[] recipients = { "user1" };
/* 639 */     TestUtil.addMessageLmtp(recipients, "user1", content);
/* 640 */     String query = "in:inbox subject:\"" + subject + "\"";
/* 641 */     assertEquals(1, TestUtil.search(mbox, query).size());
/*     */     
/*     */ 
/* 644 */     content = "Resent-Message-ID: " + System.currentTimeMillis() + "\r\n" + content;
/* 645 */     TestUtil.addMessageLmtp(recipients, "user1", content);
/* 646 */     assertEquals(2, TestUtil.search(mbox, query).size());
/*     */     
/*     */ 
/* 649 */     content = "Resent-Message-ID: " + System.currentTimeMillis() + "\r\n" + content;
/* 650 */     TestUtil.addMessageLmtp(recipients, "user1", content);
/* 651 */     assertEquals(3, TestUtil.search(mbox, query).size());
/*     */     
/*     */ 
/* 654 */     TestUtil.addMessageLmtp(recipients, "user1", content);
/* 655 */     assertEquals(3, TestUtil.search(mbox, query).size());
/*     */   }
/*     */   
/*     */   public void testFinalDotNotSent() throws Exception
/*     */   {
/* 660 */     ZMailbox mbox = TestUtil.getZMailbox("user1");
/* 661 */     LmtpClient lmtpClient = new LmtpClient("localhost", Provisioning.getInstance().getLocalServer().getIntAttr("zimbraLmtpBindPort", 7025));
/*     */     
/*     */ 
/* 664 */     assertTrue(lmtpClient.getResponse(), lmtpClient.replyOk());
/* 665 */     lmtpClient.sendLine("LHLO " + LC.zimbra_server_hostname.value());
/* 666 */     assertTrue(lmtpClient.getResponse(), lmtpClient.replyOk());
/*     */     
/* 668 */     if (lmtpClient.getResponse().contains("STARTTLS")) {
/* 669 */       lmtpClient.startTLS();
/* 670 */       lmtpClient.sendLine("LHLO " + LC.zimbra_server_hostname.value());
/* 671 */       assertTrue(lmtpClient.getResponse(), lmtpClient.replyOk());
/*     */     }
/* 673 */     lmtpClient.sendLine("MAIL FROM:<" + TestUtil.addDomainIfNecessary("user1") + ">");
/* 674 */     assertTrue(lmtpClient.getResponse(), lmtpClient.replyOk());
/* 675 */     lmtpClient.sendLine("RCPT TO:<" + TestUtil.addDomainIfNecessary("user1") + ">");
/* 676 */     assertTrue(lmtpClient.getResponse(), lmtpClient.replyOk());
/* 677 */     lmtpClient.sendLine("DATA");
/* 678 */     assertTrue(lmtpClient.getResponse(), lmtpClient.replyOk());
/* 679 */     String subject = NAME_PREFIX + " testFinalDotNotSent";
/* 680 */     lmtpClient.sendLine("Subject: " + subject);
/* 681 */     lmtpClient.abruptClose();
/*     */     
/* 683 */     Thread.sleep(1000L);
/* 684 */     List<ZMessage> msgs = TestUtil.search(mbox, "in:inbox " + subject);
/* 685 */     assertTrue("msg got delivered via LMTP even though <CRLF>.<CRLF> was not received", msgs.isEmpty());
/*     */   }
/*     */   
/*     */   public void testStartTLSSuccess() throws Exception {
/* 689 */     ZMailbox mbox = TestUtil.getZMailbox("user1");
/* 690 */     LmtpClient lmtpClient = new LmtpClient("localhost", Provisioning.getInstance().getLocalServer().getIntAttr("zimbraLmtpBindPort", 7025));
/*     */     
/*     */ 
/* 693 */     assertTrue(lmtpClient.getResponse(), lmtpClient.replyOk());
/* 694 */     lmtpClient.sendLine("LHLO " + LC.zimbra_server_hostname.value());
/* 695 */     assertTrue(lmtpClient.getResponse(), lmtpClient.replyOk());
/* 696 */     if (lmtpClient.getResponse().contains("STARTTLS")) {
/* 697 */       lmtpClient.startTLS();
/* 698 */       lmtpClient.sendLine("LHLO " + LC.zimbra_server_hostname.value());
/* 699 */       assertTrue(lmtpClient.getResponse(), lmtpClient.replyOk());
/*     */     }
/* 701 */     lmtpClient.sendLine("MAIL FROM:<" + TestUtil.addDomainIfNecessary("user1") + ">");
/* 702 */     assertTrue(lmtpClient.getResponse(), lmtpClient.replyOk());
/* 703 */     lmtpClient.sendLine("RCPT TO:<" + TestUtil.addDomainIfNecessary("user1") + ">");
/* 704 */     assertTrue(lmtpClient.getResponse(), lmtpClient.replyOk());
/* 705 */     lmtpClient.sendLine("DATA");
/* 706 */     assertTrue(lmtpClient.getResponse(), lmtpClient.replyOk());
/* 707 */     String subject = NAME_PREFIX + " testFinalDotNotSent";
/* 708 */     lmtpClient.sendLine("Subject: " + subject);
/* 709 */     lmtpClient.abruptClose();
/*     */     
/* 711 */     Thread.sleep(1000L);
/* 712 */     List<ZMessage> msgs = TestUtil.search(mbox, "in:inbox " + subject);
/* 713 */     assertTrue("msg got delivered via LMTP even though <CRLF>.<CRLF> was not received", msgs.isEmpty());
/*     */   }
/*     */   
/*     */   public void testServeShouldNotPublishStartTlsOnSecondLlhoCommand() throws Exception {
/* 717 */     LmtpClient lmtpClient = new LmtpClient("localhost", Provisioning.getInstance().getLocalServer().getIntAttr("zimbraLmtpBindPort", 7025));
/*     */     
/*     */ 
/* 720 */     assertTrue(lmtpClient.getResponse(), lmtpClient.replyOk());
/* 721 */     lmtpClient.sendLine("LHLO " + LC.zimbra_server_hostname.value());
/* 722 */     assertTrue(lmtpClient.getResponse(), lmtpClient.replyOk());
/* 723 */     if (lmtpClient.getResponse().contains("STARTTLS")) {
/* 724 */       lmtpClient.startTLS();
/* 725 */       lmtpClient.sendLine("LHLO " + LC.zimbra_server_hostname.value());
/* 726 */       assertTrue(lmtpClient.getResponse(), lmtpClient.replyOk());
/* 727 */       assertTrue(lmtpClient.getResponse(), !lmtpClient.getResponse().contains("STARTTLS"));
/*     */     }
/* 729 */     lmtpClient.abruptClose();
/*     */   }
/*     */   
/*     */   public void testLhloNotSendByClient() throws Exception {
/* 733 */     String[] commands = { "NOOP", "RSET", "VRFY user1", "MAIL FROM:<" + TestUtil.addDomainIfNecessary("user1") + ">" };
/*     */     
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 740 */     Provisioning prov = Provisioning.getInstance();
/*     */     
/*     */ 
/* 743 */     boolean lhloRequired = prov.getLocalServer().getBooleanAttr("zimbraLmtpLHLORequired", true);
/* 744 */     for (String command : commands) {
/* 745 */       LmtpClient lmtpClient = new LmtpClient("localhost", Provisioning.getInstance().getLocalServer().getIntAttr("zimbraLmtpBindPort", 7025));
/*     */       
/* 747 */       assertTrue(lmtpClient.getResponse(), lmtpClient.replyOk());
/* 748 */       lmtpClient.sendLine(command);
/* 749 */       boolean replyOk = lmtpClient.replyOk();
/* 750 */       assertTrue("Response :" + lmtpClient.getResponse() + " for command :" + command, lhloRequired ? false : !replyOk ? true : replyOk);
/* 751 */       lmtpClient.abruptClose();
/*     */     }
/*     */   }
/*     */   
/*     */   public void testLhloNotSendByClientAfterStartTLS() throws Exception {
/* 756 */     String[] commands = { "NOOP", "RSET", "VRFY user1", "MAIL FROM:<" + TestUtil.addDomainIfNecessary("user1") + ">" };
/*     */     
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 763 */     Provisioning prov = Provisioning.getInstance();
/* 764 */     boolean lhloRequired = prov.getLocalServer().getBooleanAttr("zimbraLmtpLHLORequired", true);
/*     */     
/*     */ 
/* 767 */     for (String command : commands) {
/* 768 */       LmtpClient lmtpClient = new LmtpClient("localhost", Provisioning.getInstance().getLocalServer().getIntAttr("zimbraLmtpBindPort", 7025));
/*     */       
/*     */ 
/* 771 */       assertTrue(lmtpClient.getResponse(), lmtpClient.replyOk());
/* 772 */       lmtpClient.sendLine("LHLO " + LC.zimbra_server_hostname.value());
/* 773 */       assertTrue(lmtpClient.getResponse(), lmtpClient.replyOk());
/* 774 */       if (lmtpClient.getResponse().contains("STARTTLS")) {
/* 775 */         lmtpClient.startTLS();
/* 776 */         lmtpClient.sendLine(command);
/* 777 */         boolean replyOk = lmtpClient.replyOk();
/* 778 */         assertTrue("Response :" + lmtpClient.getResponse() + " for command :" + command, lhloRequired ? false : !replyOk ? true : replyOk);
/*     */       }
/* 780 */       lmtpClient.abruptClose();
/*     */     }
/*     */   }
/*     */   
/*     */   public void testErrorWhenNoStartTlsOnSslEnforcedByServer() throws Exception {
/* 785 */     boolean tlsEnforcedByServer = LC.zimbra_require_interprocess_security.booleanValue();
/* 786 */     if (tlsEnforcedByServer) {
/* 787 */       LmtpClient lmtpClient = new LmtpClient("localhost", Provisioning.getInstance().getLocalServer().getIntAttr("zimbraLmtpBindPort", 7025));
/*     */       
/*     */ 
/* 790 */       assertTrue(lmtpClient.getResponse(), lmtpClient.replyOk());
/* 791 */       lmtpClient.sendLine("LHLO " + LC.zimbra_server_hostname.value());
/* 792 */       assertTrue(lmtpClient.getResponse(), lmtpClient.replyOk());
/* 793 */       if (lmtpClient.getResponse().contains("STARTTLS")) {
/* 794 */         lmtpClient.sendLine("MAIL FROM:<" + TestUtil.addDomainIfNecessary("user1") + ">");
/* 795 */         assertTrue(lmtpClient.getResponse(), !lmtpClient.replyOk());
/*     */       }
/* 797 */       lmtpClient.abruptClose();
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   public void tearDown()
/*     */     throws Exception
/*     */   {
/* 806 */     setQuotaWarnPercent(this.originalWarnPercent);
/* 807 */     setQuotaWarnInterval(this.originalWarnInterval);
/* 808 */     TestUtil.setServerAttr("zimbraMailDiskStreamingThreshold", this.originalServerDiskThreshold);
/* 809 */     TestUtil.setConfigAttr("zimbraMailDiskStreamingThreshold", this.originalConfigDiskThreshold);
/* 810 */     TestUtil.setAccountAttr("user1", "zimbraMailQuota", this.originalQuota);
/* 811 */     TestUtil.setAccountAttr("user1", "zimbraMailAllowReceiveButNotSendWhenOverQuota", this.originalAllowReceiveButNotSendWhenOverQuota);
/*     */     
/* 813 */     TestUtil.setConfigAttr("zimbraMessageIdDedupeCacheSize", this.originalDedupeCacheSize);
/* 814 */     TestUtil.setConfigAttr("zimbraMessageIdDedupeCacheTimeout", this.originalDedupeCacheTimeout);
/* 815 */     TestUtil.setAccountAttr("user1", "zimbraPrefMessageIdDedupingEnabled", this.originalDedupingEnabled);
/* 816 */     cleanUp();
/*     */   }
/*     */   
/*     */   private void cleanUp() throws Exception
/*     */   {
/* 821 */     TestUtil.deleteTestData("user1", NAME_PREFIX);
/* 822 */     TestUtil.deleteTestData("user1", "Quota warning");
/* 823 */     TestUtil.deleteTestData("user2", NAME_PREFIX);
/*     */   }
/*     */   
/*     */   public static void main(String[] args) throws Exception
/*     */   {
/* 828 */     TestUtil.cliSetup();
/* 829 */     TestUtil.runTest(TestLmtp.class);
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/qa/unittest/TestLmtp.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */